<?php
namespace App\Models\Account;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Request;
use App\Admin\Extra\Search\Html;

/**
 * 行为体力分析
 * 仅仅限于钻石购买体力。
 */
class BehaviorThew extends  Model{

    protected $table='behavior_log';
    protected $_db_select = 'remote_bgsi';

    public $_max_count = 0;
    

    public function __construct()
    {
        \App\Models\Area::RemoteBgsiSelectByArea(Request::get('area'));
    }

    // 按时间为条件，购买次数分组， 主要查人数；

    public function paginate() {
        $perPage = Request::get('per_page', 100);

        $page = Request::get('page', 1);

        $start = ($page-1)*$perPage;     
        
        $rw = $this->selectGroup();
        self::SourceCondition($rw);
        // $rw->groupBy("op_module", "op");
        $rw->groupBy("player_id");
        $total = $rw->count();
        $rw->orderBy('op_time', "DESC");
        $rw->skip($start)->take($perPage);
        $data =  $rw->get();
        $data = $data->toArray();

        // \App\Models\Account\Behavior::attachItemWithOpAlias($data);
        $data_sta = [];
        foreach ($data as $obj) {
            $data_sta[$obj->times]['num']++;
            $data_sta[$obj->times]['times'] = $obj->times;
            if (!count($data_sta[$obj->times]['player_id'] )) {
                $data_sta[$obj->times]['player_id'] = [];
            }
            $data_sta[$obj->times]['player_id'][] = $obj->player_id;
        }
        $total = count($data_sta);
        // $list = static::hydrate($data_sta);

        $paginator = new LengthAwarePaginator($data_sta, $total, $perPage);

        $paginator->setPath(url()->current());
        // \Log::debug(__METHOD__. print_r($data_sta, true));
        \Log::debug(__METHOD__, [$rw->toSql().'-----------------------']);
        return $paginator;
    }

    public function selectdb($select = ''){
        if ($select) {
            return $this->_db_select = $select;
        }
        return $this->_db_select;
    }
    public function selectGroup() {
        return \DB::connection($this->selectdb())->table($this->table)
        ->select('op','op_module', \DB::raw('count(id) as times'), \DB::raw('sum(op_val_1) as diamonds'), 'player_id');
    }
    public static function SourceCondition($rw) {
        $cond = Request::all();

        if ($cond['player_id']){
            $pids = explode(',', $cond['player_id']);
            $rw->whereIn("player_id", $pids);
        }
        if ($cond['op_module']){
            $rw->where("op_module", $cond['op_module']);
        }
        if ($cond['level']){
            $rw->where("level",'<=', $cond['level']);
        }
        if ($cond['vip']){
            $rw->where("op_val_3",'<=', $cond['vip']);
        }
        // if ($cond['area']){
        //     $rw->where("area", $cond['area']);
        // }
        if (empty($cond['op_time'])) {
            $cond['op_time'] = ['start' => time() - 30 * 86400];
        }
        if (is_array($cond['op_time'])) {
            $start = \App\ModelStatistic\StatisticCommon::GetIntTime($cond['op_time']['start']) ?: 0;
            $rw->where("op_time", ">", $start);
            $end = \App\ModelStatistic\StatisticCommon::GetIntTime($cond['op_time']['end']) ?: 0;
            if ($end) {
                $rw->where("op_time", "<", $end);
            }
        }
    }
}